import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;


public class FileUtils {

	public static void appendToFile(String content) {
		
    	FileWriter fw;
		try {
			fw = new FileWriter("cmds.cps", true);
	    	BufferedWriter bw = new BufferedWriter(fw);
	    	bw.write(content);
	    	bw.close();
		} catch (IOException e) {
			System.out.println("ERROR: ioexception when appending to file.");
			e.printStackTrace();
		}
   }
	
	public static Properties fileToProps(String path){
		
		System.out.println(path);
		Properties props = new Properties();
		try{


			FileReader reader = new FileReader(path);
		    BufferedReader br = new BufferedReader(reader);
		    String s;
		    //Read line by line
		    while ((s = br.readLine()) != null) {
		      // Gigantic hardcoded parser
		      if (s.startsWith(Constants.OPT_DEVICE_NAMES)){
		    	  props.OPT_DEVICE_NAMES.add(s.split("=")[1].trim().toLowerCase());
		    	  System.out.println("Added device name, total=" + props.OPT_DEVICE_NAMES.size());
		      }else if (s.startsWith(Constants.OPT_EXE_REQUIRED)){
		    	  props.OPT_EXE_REQUIRED=(s.split("=")[1].trim().equalsIgnoreCase("true"))?true:false;
		      }else if (s.startsWith(Constants.OPT_EXEPATH)){
		    	  if (props.OPT_EXE_REQUIRED) props.OPT_EXEPATH=s.split("=")[1];
		      }else if (s.startsWith(Constants.OPT_INPUTPATH)){
		    	  props.OPT_INPUTPATH=s.split("=")[1].trim().toLowerCase();
		      }else if (s.startsWith(Constants.OPT_OUTPUTPATH)){
		    	  props.OPT_OUTPUTPATH=s.split("=")[1].trim().toLowerCase();
		      }else if (s.startsWith(Constants.OPT_LIB_IN)){
		    	  if (props.OPT_TRANSLATEINPUTS) {
		    		  props.OPT_LIB_IN.add(s.split("=")[1]);
		    		  System.out.println("Added input translation, total=" + props.OPT_LIB_IN.size());
		    	  }
		      }else if (s.startsWith(Constants.OPT_LIB_OUT)){
		    	  if (props.OPT_TRANSLATEOUTPUTS) props.OPT_LIB_OUT.add(s.split("=")[1]);
		    	  System.out.println("Added output translation, total=" + props.OPT_LIB_OUT.size());
		      }else if (s.startsWith(Constants.OPT_TRANSLATEINPUTS)){
		    	  props.OPT_TRANSLATEINPUTS=(s.split("=")[1].trim().equalsIgnoreCase("true"))?true:false;
		      }else if (s.startsWith(Constants.OPT_TRANSLATEOUTPUTS)){
		    	  props.OPT_TRANSLATEOUTPUTS=(s.split("=")[1].trim().equalsIgnoreCase("true"))?true:false;
		      }else if (s.startsWith(Constants.OPT_USER_COMMAND_EMAIL)){
		    	  props.OPT_USER_COMMAND_EMAIL=s.split("=")[1].trim().toLowerCase();
		      }else if (s.startsWith(Constants.OPT_USER_COMMAND_PWD)){
		    	  props.OPT_USER_COMMAND_PWD=s.split("=")[1].trim().toLowerCase();
		    	  System.out.println(props.OPT_USER_COMMAND_PWD);
		      }else if (s.startsWith(Constants.OPT_USER_MOBILE_EMAIL)){
		    	  props.OPT_USER_MOBILE_EMAIL=s.split("=")[1].trim().toLowerCase();
		      } else if (s.startsWith(Constants.OPT_ALLOWED_USER)) {
		    	  props.OPT_ALLOWED_USERS.add(s.split("=")[1].trim().toLowerCase());
		      }
		    }
		    //Close the input stream
		    br.close();
		    }catch (Exception e){//Catch exception if any
		      System.err.println("Error: " + e.getMessage());
		    }

		return props;
	}

}
